package cn.mysite.mycompany.controller;


import cn.mysite.mycompany.entity.Cart;
import cn.mysite.mycompany.entity.User;
import cn.mysite.mycompany.service.CartService;
import cn.mysite.mycompany.service.UserAddressService;
import cn.mysite.mycompany.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author  ChenChongFei
 * @since 2021-03-13
 */
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private UserService userService;
    @Autowired
    private CartService cartService;

    @Autowired
    private UserAddressService userAddressService;

    @GetMapping("/add/{productId}/{price}/{quantity}")
    public String add(
            @PathVariable("productId")Integer productId,
            @PathVariable("quantity") Integer quantity,
            @PathVariable("price")Float price,
            HttpSession session){

        Cart cart = new Cart();
        cart.setProductId(productId);
        cart.setQuantity(quantity);
        cart.setCost(quantity*price);
        User user=(User)session.getAttribute("user");
        cart.setUserId(user.getId());
        try {
            if (cartService.save(cart))
                return "redirect:/cart/findAllCart";
        }catch (Exception e){
            return "redirect:/productCategory/list";
        }
        return null;
    }

    @GetMapping("/findAllCart")
    public ModelAndView findAllCart(HttpSession session){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("settlement1");
        User user=(User)session.getAttribute("user");
        mv.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        return mv;
    }

    @GetMapping("deleteById/{cartId}")
    public String deleteCart(@PathVariable("cartId") Integer cartId){

        Cart cart=cartService.getById(cartId);
        cartService.alterByStep(cart,-cart.getQuantity());
        cartService.removeById(cartId);

        return "redirect:/cart/findAllCart";
    }

    //跳转到确认订单
    @GetMapping("/settlement2")
    public ModelAndView settlement2(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement2");
        User user = (User)session.getAttribute("user");
        modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",user.getId());
        modelAndView.addObject("addressList",userAddressService.list(wrapper));
        return modelAndView;
    }

    @ResponseBody
    @PostMapping("/update/{id}/{quantity}/{cost}")
    public String updateCart(
            @PathVariable("id") Integer id,
            @PathVariable("quantity") Integer quantity,
            @PathVariable("cost") Float cost
    ){
        Cart cart=cartService.getById(id);
        cart.setCost(cost);
        cart.setQuantity(cart.getQuantity()+quantity);
        if(cartService.alterByStep(cart,quantity))
            return "success";
        else
            return "fail";

    }

}

